<?php
/**
 * Settings.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class */
require_once 'Madoqua/Form.php';

/**
 * BlogAdmin_Theme_Settings
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_Theme_Settings extends Madoqua_Form
{
    /**
     * constructor
     *
     * @param array $arguments
     */
    public function __construct($arguments)
    {
        parent::__construct();

        if (!isset($arguments['theme'])) {
            throw new Exception('Theme must be set');
        }

        $theme = $arguments['theme'];
        //the theme
        
        foreach($theme->config->settings as $name => $setting) {

            $required = (!empty($setting->required)) ? (bool) $setting->required : false;
            
            switch ($setting->type) {
                case 'int':
                case 'text':
                    $this->addElement('text', $name, array(
                        'required' => $required,
                        'label' => (string) $setting->description
                    ));
                    break;
                case 'html':
                    $this->addElement('textarea', $name, array(
                        'required' => $required,
                        'label' => (string) $setting->description
                    ));
                    break;
            }
        }
        //add the required elements

        foreach($theme->config->settings as $name => $setting) {
            
            if ($setting->type == 'int') {
                //integer field
                 $this->getElement($name)->addValidator(
                    'Digits'
                );
            }
            
            if ($setting->validators) {
                //i can has validators?
                
                foreach($setting->validators as $validator) {
                    //for each requested validator for this element
                    
                    $options = array();
                    if ($validator->arguments) {
                        foreach($validator->arguments as $argument) {
                            $options[] = trim((string) $argument);
                        }
                    }
                    //arguments for the validator
                    
                    $this->getElement($name)->addValidator(
                        trim((string) $validator->validator),
                        false,
                        $options
                    );
                    //add it
                    
                }
            }
        }
        //add validators

        $this->addElement('submit', '_s', array(
            'label' => 'Submit'
            ));
            //submit button

            $this->setDefaults($theme->userConfig->toArray());

    }
}